package com.alit.face.koala.service.api;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alit.face.koala.app.data.AppResponse;
import com.alit.face.koala.app.data.Menu;
import com.alit.face.koala.app.data.MenuData;
import com.alit.face.koala.app.data.Page;
import com.litong.utils.projectvariable.PVU;

@Service
public class MenuService {

  private List<Menu> menuList;

  public AppResponse menu() {
    AppResponse retval = new AppResponse();
    retval.setCode(0);
    retval.setPage(new Page());
    retval.setData(getMenuData());
    return retval;
  }

  private MenuData getMenuData() {
    List<Menu> menuList = getMenuList();
    MenuData menuData = new MenuData();
    menuData.setMenu_list(menuList);
    return menuData;
  }

  private List<Menu> getMenuList() {
    if (menuList == null) {
      File file = new File(PVU.getJsonFilePath("menu_list.json"));
      String string = null;
      try {
        string = FileUtils.readFileToString(file, "UTF-8");
      } catch (IOException e) {
        e.printStackTrace();
      }
      menuList = JSON.parseArray(string, Menu.class);
    }
    return menuList;

  }
}
